/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.adapter.recyclerview;

import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import com.xuexiang.xui_lib.component.imageview.ImageLoader;
import com.xuexiang.xui_lib.component.imageview.RadiusImageView;

import java.io.IOException;
import java.util.HashMap;

/**
 * 通用的ViewHolder
 *
 * @author xuexiang
 * @since 2019/4/6 下午3:45
 */
public class RecyclerViewHolder {
    private HashMap<Integer, Component> mViews;
    private Component itemView;

    public RecyclerViewHolder(Component component) {
        mViews = new HashMap<>();
        itemView = component;
    }

    public Component getItemView() {
        return itemView;
    }

    public <T extends Component> T findViewById(int viewId) {
        Component view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findComponentById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public Component getView(int viewId) {
        return findViewById(viewId);
    }

    public Text getTextView(int viewId) {
        return (Text) getView(viewId);
    }

    public Button getButton(int viewId) {
        return (Button) getView(viewId);
    }

    public Image getImageView(int viewId) {
        return (Image) getView(viewId);
    }

    public TextField getEditText(int viewId) {
        return (TextField) getView(viewId);
    }

    public final Context getContext() {
        return itemView.getContext();
    }

    /**
     * 寻找控件
     *
     * @param id
     * @return Component
     */
    public Component findView(int id) {
        return id == 0 ? itemView : findViewById(id);
    }

    /**
     * 设置文字
     *
     * @param id
     * @param sequence
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder text(int id, CharSequence sequence) {
        Component view = findView(id);
        if (view instanceof Text) {
            ((Text) view).setText(String.valueOf(sequence));
        }
        return this;
    }

    /**
     * 设置文字
     *
     * @param id
     * @param stringRes
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder text(int id, int stringRes) {
        Component view = findView(id);
        if (view instanceof Text) {
            ((Text) view).setText(stringRes);
        }
        return this;
    }

    /**
     * 设置文字的颜色
     *
     * @param id
     * @param colorId
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder textColorId(int id, int colorId) {
        Component view = findView(id);
        if (view instanceof Text) {
            ((Text) view).setTextColor(new Color(view.getContext().getColor(colorId)));
        }
        return this;
    }

    /**
     * 设置图片
     *
     * @param id
     * @param imageId
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder image(int id, int imageId) {
        Component view = findView(id);
        if (view instanceof Image) {
            ((Image) view).setPixelMap(imageId);
        }
        return this;
    }

    /**
     * 设置图片
     *
     * @param id
     * @param drawable
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder image(int id, Element drawable) {
        Component view = findView(id);
        if (view instanceof Image) {
            ((Image) view).setImageElement(drawable);
        }
        return this;
    }

    /**
     * 设置图片
     *
     * @param id
     * @param uri 图片资源
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder image(int id, Object uri) {
        Component view = findView(id);
        if (view instanceof Image) {
            ImageLoader.get().loadImage((Image) view, uri);
        }
        return this;
    }

    /**
     * 设置布局内控件的点击事件【包含索引】
     *
     * @param id
     * @param listener
     * @param item
     * @param position
     * @return RecyclerViewHolder
     */
    public <T> RecyclerViewHolder viewClick(int id, final OnViewItemClickListener<T> listener, final T item, final int position) {
        Component view = findView(id);
        if (listener != null) {
            view.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    listener.onViewItemClick(component, item, position);
                }
            });
        }
        return this;
    }

    /**
     * 设置控件的点击监听
     *
     * @param id
     * @param listener
     * @return RecyclerViewHolder RecyclerViewHolder
     */
    public RecyclerViewHolder click(int id, final Component.ClickedListener listener) {
        Component view = findView(id);
        if (listener != null) {
            view.setClickedListener(listener);
        }
        return this;
    }

    /**
     * 设置控件是否可显示
     *
     * @param id
     * @param visibility
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder visible(int id, int visibility) {
        Component view = findView(id);
        view.setVisibility(visibility);
        return this;
    }

    /**
     * 设置输入框是否可编辑
     *
     * @param id
     * @param enable
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder enable(int id, boolean enable) {
        Component view = findView(id);
        view.setEnabled(enable);
        if (view instanceof TextField) {
            int focusable = enable ? Component.FOCUS_ENABLE : Component.FOCUS_DISABLE;
            view.setFocusable(focusable);
            view.setTouchFocusable(enable);
        }
        return this;
    }

    /**
     * 这是控件选中状态
     *
     * @param id
     * @param checked
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder checked(int id, boolean checked) {
        Component view = findView(id);
        if (view instanceof AbsButton) {
            ((AbsButton) view).setChecked(checked);
        }
        return this;
    }

    /**
     * 设置控件选择监听
     *
     * @param id
     * @param listener
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder checkedListener(int id, AbsButton.CheckedStateChangedListener listener) {
        Component view = findView(id);
        if (view instanceof AbsButton) {
            ((AbsButton) view).setCheckedStateChangedListener(listener);
        }
        return this;
    }

    /**
     * 设置控件是否选中
     *
     * @param id
     * @param selected
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder select(int id, boolean selected) {
        Component view = findView(id);
        view.setSelected(selected);
        return this;
    }

    /**
     * 设置文字变化监听
     *
     * @param id
     * @param watcher
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder textListener(int id, Text.TextObserver watcher) {
        Component view = findView(id);
        if (view instanceof Text) {
            ((Text) view).addTextObserver(watcher);
        }
        return this;
    }

    /**
     * 设置背景
     *
     * @param viewId
     * @param resId
     * @return RecyclerViewHolder
     */
    public RecyclerViewHolder backgroundResId(int viewId, int resId) {
        Component view = findView(viewId);
        try {
            view.setBackground(new PixelMapElement(view.getContext().getResourceManager().getResource(resId)));
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 清除控件缓存
     */
    public void clearViews() {
        if (mViews != null) {
            mViews.clear();
        }
    }

    /**
     * 布局内控件点击事件
     */
    public interface OnViewItemClickListener<T> {
        /**
         * 控件被点击
         *
         * @param view 被点击的控件
         * @param item 数据
         * @param position 索引
         */
        void onViewItemClick(Component view, T item, int position);
    }
}
